﻿@{
    ViewBag.Title = "ItemObjectForm";
    Layout = "~/Views/Shared/_LayoutIndex.cshtml";
}


<script>
    var ItemId = request('ItemId');
    var TypeId = request('TypeId');
    var OrganizeId = request('OrganizeId');
    var list = [];
    var ids = "";
    $(function () {
        InitialPage();
        GetGrid();
    });
    //初始化页面
    function InitialPage() {
        //resize重设(表格、树形)宽高
        $(window).resize(function (e) {
            window.setTimeout(function () {
                $('#gridTable').setGridWidth(($('.gridPanel').width()));
                $("#gridTable").setGridHeight($(window).height() - 114.5);
            }, 200);
            e.stopPropagation();
        });
    }
    //加载表格
    function GetGrid() {
        var selectedRowIndex = 0;
        var $gridTable = $("#gridTable");
        var _url = "";
        var CML = [];
        if (!!TypeId) {
            $.SetForm({
                url: "../../SystemManage/DataItemDetail/GetForm",
                param: { keyValue: TypeId },
                success: function (data) {
                    switch (data.ItemCode) {
                        case "ZX_XJ": _url = "../../PropertyManage/CS_Polling/GetDecorateRelationOIListJson?ItemId=" + ItemId + "&TypeId=" + TypeId + "&OrganizeId=" + OrganizeId; CML = [
                            { label: '主键', name: 'ObjectId', hidden: true },
                            { label: '主键', name: 'OIId', hidden: true },
                            { label: '主键', name: 'isCheck', hidden: true },
                            { label: '主键', name: 'OrganizeId', hidden: true },
                            { label: '对象名称', name: 'ObjName', index: 'ObjName', width: 200, align: 'center', sortable: false },
                            { label: '编码', name: 'ObjCode', index: 'ObjCode', width: 200, align: 'center', sortable: false },
                            { label: '执行次数', name: 'Frequency', index: 'Frequency', editable: true, editrules: { edithidden: true, required: true, number: true }, edittype: "text", width: 150, align: 'center', sortable: false }
                        ]; break;
                        case "SB_XJ":

                            break;
                        case "SP_XJ": _url = "../../PropertyManage/CS_Polling/GetRoomRelationOIListJson?ItemId=" + ItemId + "&TypeId=" + TypeId + "&OrganizeId=" + OrganizeId;; CML = [
                            { label: '主键', name: 'ObjectId', hidden: true },
                            { label: '主键', name: 'OIId', hidden: true },
                            { label: '主键', name: 'isCheck', hidden: true },
                            { label: '主键', name: 'OrganizeId', hidden: true },
                            { label: '对象名称', name: 'ObjName', index: 'ObjName', width: 200, align: 'center', sortable: false },
                            { label: '编码', name: 'ObjCode', index: 'ObjCode', width: 200, align: 'center', sortable: false },
                            { label: '执行次数', name: 'Frequency', index: 'Frequency', editable: true, editrules: { edithidden: true, required: true, number: true }, edittype: "text", width: 150, align: 'center', sortable: false }
                        ]; break;
                        default: _url = "../../PropertyManage/CS_Polling/GetAreaRelationOIListJson?ItemId=" + ItemId + "&TypeId=" + TypeId + "&OrganizeId=" + OrganizeId;;
                            CML = [
                            { label: '主键', name: 'ObjectId', hidden: true },
                            { label: '主键', name: 'OIId', hidden: true },
                            { label: '主键', name: 'isCheck', hidden: true },
                            { label: '主键', name: 'OrganizeId', hidden: true },
                            { label: '对象名称', name: 'ObjName', index: 'ObjName', width: 200, align: 'center', sortable: false },
                            { label: '编码', name: 'ObjCode', index: 'ObjCode', width: 200, align: 'center', sortable: false },
                            {
                                label: "标识方式", name: "FlagWay", index: "FlagWay", width: 300, align: "center",
                                formatter: function (cellvalue, options, rowObject) {
                                    switch (cellvalue) {
                                        case 0: return "无标识";
                                        case 1: return "rfid芯片";
                                        case 2: return "条码";
                                        case 3: return "二维码";
                                        case 4: return "beacon";
                                    }
                                }
                            },
                             { label: '执行次数', name: 'Frequency', index: 'Frequency', editable: true, editrules: { edithidden: true, required: true, number: true }, edittype: "text", width: 150, align: 'center', sortable: false }
                            ];
                            break;

                    }
                }
            });
        }
        $gridTable.jqGrid({
            url: _url,
            datatype: "json",
            height: $(window).height() - 114.5,
            autowidth: true,
            multiselect: true,
            cellEdit: true,
            cellsubmit: 'rclientArray',
            colModel: CML,
            rowNum: "10000",
            rownumbers: true,
            onSelectRow: function (rowid, status) {
                if (!!$gridTable.getRowData(rowid).OIId) {
                    if (status) {
                        list.push($gridTable.getRowData(rowid));
                        ids.replace($gridTable.getRowData(rowid).OIId + ",", "");
                    }
                    else {
                        $.each(list, function (i, n) {
                            if ($gridTable.getRowData(rowid).ObjectId == n.ObjectId) {
                                list.splice(i, 1);
                                return false;
                            }
                        });
                        ids += $gridTable.getRowData(rowid).OIId + ","
                    }
                }
                else {
                    if (status) {
                        list.push($gridTable.getRowData(rowid));
                    }
                    else {
                        $.each(list, function (i, n) {
                            if ($gridTable.getRowData(rowid).ObjectId == n.ObjectId) {
                                list.splice(i, 1);
                                return false;
                            }
                        });
                    }
                }
            },
            onSelectAll: function (arowid, status) {
                for (var i = 0; i < list.length; i++) {
                    list.splice(i, 1);//从下标为i的元素开始，连续删除1个元素
                    i--;//因为删除下标为i的元素后，该位置又被新的元素所占据，所以要重新检测该位置
                }
                var rowIds = $gridTable.jqGrid('getDataIDs');
                $.each(rowIds, function (i, n) {
                    if (!!$gridTable.getRowData(n).OIId) {
                        if (status) {
                            list.push($gridTable.getRowData(n));
                            ids.replace($gridTable.getRowData(n).OIId + ",", "");
                        }
                        else {
                            ids += $gridTable.getRowData(n).OIId + ","
                        }
                    }
                    else {
                        if (status) {
                            list.push($gridTable.getRowData(n));
                        }
                        else {
                            $.each(list, function (i, a) {
                                if ($gridTable.getRowData(n).ObjectId == a.ObjectId) {
                                    list.splice(i, 1);
                                    return false;
                                }
                            });
                        }
                    }
                });
            },
            beforeSelectRow: function (rowid, e) {
                var $myGrid = $(this),
                    i = $.jgrid.getCellIndex($(e.target).closest('td')[0]),
                    cm = $gridTable.jqGrid('getGridParam', 'colModel');
                return (cm[i].name === 'cb');
            },
            gridComplete: function () {
                //$("#" + this.id).setSelection(selectedRowIndex, false);
                var rowIds = $gridTable.jqGrid('getDataIDs');
                for (var k = 0; k < rowIds.length; k++) {
                    var data = $gridTable.getRowData(rowIds[k]);
                    if (data.isCheck == "true") {
                        $gridTable.setSelection(rowIds[k]);
                    }
                }
            },
            ondblClickRow: function (rowid, iRow, iCol, e) {
                if (rowid && rowid !== selectedRowIndex && selectedRowIndex != 0) {
                    $gridTable.saveRow(selectedRowIndex);
                    $.each(list, function (i, n) {
                        if ($gridTable.getRowData(rowid).ObjectId == n.ObjectId) {
                            list[i].Frequency = $gridTable.getRowData(rowid).Frequency;
                            return false;
                        }
                    });
                }
                $gridTable.editRow(rowid, {
                    keys: true, successfunc: function (response) {
                        $gridTable.jqGrid("saveRow", rowid);
                        $.each(list, function (i, n) {
                            if ($gridTable.getRowData(rowid).ObjectId == n.ObjectId) {
                                list[i].Frequency = $gridTable.getRowData(rowid).Frequency;
                                return false;
                            }
                        });
                        return true;
                    }
                });
                selectedRowIndex = rowid;
            }
        });
    }

    //保存表单
    function AcceptClick() {
        if (list.length < 1) {
            dialogMsg('最少选择一条数据', 0);
            return false;
        }
        var Objlist = [];
        $.each(list, function (i, n) {
            var a = { Id: n.OIId, ItemId: ItemId, TypeId: TypeId, ObjectId: n.ObjectId, Frenquency: n.Frequency, OrganizeId: n.OrganizeId };
            Objlist.push(a);
        })

        $.SaveForm({
            url: "../../PropertyManage/CS_Polling/SaveRelationOIForm",
            param: { objlist: JSON.stringify(Objlist), ids: ids },
            loading: "正在保存数据...",
            success: function () {
                $.currentIframe().$("#gridTable").resetSelection();
                $.currentIframe().$("#gridTable").trigger("reloadGrid");
                //top.frames["ItemDetailIndex"].$("#gridTable").trigger("reloadGrid");
            }
        })
    }

</script>
<div class="gridPanel">
    <table id="gridTable"></table>
</div>


